package cn.itsource.hrm;

import cn.itsource.hrm.document.CourseDoc;
import cn.itsource.hrm.repository.CourseRepostory;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Optional;
//
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchServerApp.class)
public class EStest {

    @Autowired
    private ElasticsearchTemplate template;

    @Autowired
    private CourseRepostory repository;

    // 创建索引库 和映射关系 
    
    @Test
    public void createIndex(){
        //创建索引库 
        template.createIndex(CourseDoc.class);
        // 创建映射关系  
        template.putMapping(CourseDoc.class);
    }
//
////    @Test
////    public void addCourse(){
////        for (int i = 0; i < 20; i++) {
////            CourseDoc doc = new CourseDoc(i*2+2L, "SpringCloud 高级学习笔记",
////                    "源码时代", i*400f);
////            repository.save(doc);
////        }
////
////        System.out.println("保存成功！");
////    }
//
//    @Test
//    public void deleCourse(){
//        repository.deleteById(2L);
//        System.out.println("删除成功！");
//    }
//
//
//    @Test
//    public void find(){
//        Optional<CourseDoc> optional = repository.findById(1L);
//        System.out.println(optional.get());
//
//        System.out.println("-----------------------------");
//        Iterable<CourseDoc> docs = repository.findAll();
//        docs.forEach( System.out::println);
//    }
//
//    // 查名字中有 Java 的课程，价钱小于5000f, 用id 倒序排序 ，分页每页3，展示第2页
//    @Test
//    public  void search(){
//        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
//
//        // 复合查询 
//        // 字中有 Java 的课程
//        // 价钱小于2000f,
//        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
//        boolQueryBuilder.must(new MatchQueryBuilder("name","Java"));
//
//        boolQueryBuilder.filter(new RangeQueryBuilder("price").lte(2000F));
//        builder.withQuery(boolQueryBuilder);
//
//        // 用id 倒序排序
//        FieldSortBuilder sortBuilder = new FieldSortBuilder("id").order(SortOrder.DESC);
//        builder.withSort(sortBuilder);
//
//        //分页每页3，展示第2页
//        //builder.withPageable(PageRequest.of(1, 5));
//
//        
//        // 根据条件创建 searchQuery
//        NativeSearchQuery searchQuery = builder.build();
//        // search方法需要一个searchQuery 通过NativeSearchQueryBuilder 创建searchQuery
//        Page<CourseDoc> search = repository.search(searchQuery);
//
//        long totalElements = search.getTotalElements();//总数
//        System.out.println("总数: "+ totalElements);
//        int totalPages = search.getTotalPages(); //总页数
//        System.out.println("总页数: "+ totalPages);
//        List<CourseDoc> content = search.getContent();
//        content.forEach(System.out::println);
//    }
//    
//    
}
